import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;

public class three {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        ResourceBundle resourceBundle =  ResourceBundle.getBundle("jdbc");
        Class.forName(resourceBundle.getString("driverclass")); // 反射注册驱动

        String url = resourceBundle.getString("url");
        String password = resourceBundle.getString("password");
        String user =  resourceBundle.getString("user");
        Connection connection = DriverManager.getConnection(url, user, password);

        connection.setAutoCommit(false); // 关闭自动提交事务

        String sql  = "insert into tb_user (name, password) values (?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Scanner scanner = new Scanner(System.in);
        String user_name = scanner.next();
        String user_password = scanner.next();
        preparedStatement.setString(1, user_name);
        preparedStatement.setString(2, user_password);

        int raw = preparedStatement.executeUpdate();
        if (raw == 1) System.out.println("成功");

        sql = "delete from tb_user WHERE name = 'Wccc' and password = 'w75189456'";
        preparedStatement = connection.prepareStatement(sql);
        System.out.println(raw = preparedStatement.executeUpdate());

        sql = "select *from tb_user where name = 'Wc'";
        preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println(resultSet.getString("name") + " " + resultSet.getString("password"));
        }

        sql = "update tb_user set name = 'Wccc', password = 'w75189456' where name = '111'";
        preparedStatement = connection.prepareStatement(sql);
        System.out.println(preparedStatement.execute());

        if (preparedStatement != null)
        preparedStatement.close();

        if (connection != null) {
            connection.commit();
            connection.close();
        }

    }
}
